<template>
  <div>
    <Breadcrumb></Breadcrumb>
    <div class="header">
      <div class="form">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="产品名称">
            <el-input
              v-model="formInline.user"
              @blur="blur"
              placeholder="产品名称"
              size="small"></el-input>
          </el-form-item>
          <el-form-item label="添加时间">
            <el-date-picker
              v-model="formInline.date"
              size="small"
              type="date"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="group">
        <el-button size="small" type="warning" icon="el-icon-plus" @click="toProductPage()"
          >添加商品</el-button
        >
        <el-button size="small" type="danger" icon="el-icon-delete" @click="deleteAll()"
          >批量删除</el-button
        >
      </div>
    </div>
    <div class="content">
      <el-table
        :data="tableData"
        style="width: 100%"
        stripe
        header-cell-class-name="active-header"
        cell-class-name="table-center"
        @select="selectHandle"
        @select-all="selectHandle">
        <el-table-column type="selection" width="50"> </el-table-column>
        <el-table-column prop="id" label="商品编号" width="80"></el-table-column>
        <el-table-column prop="title" label="商品名称" width="140" show-overflow-tooltip>
          <template slot-scope="scope">
            <span
              style="color: blue; cursor: pointer"
              @click="handleLook(scope.$index, scope.row)"
              >{{ scope.row.title }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="price" label="商品价格" width="80" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="category"
          label="商品类目"
          width="100"
          show-overflow-tooltip></el-table-column>
        <el-table-column label="添加时间" width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ moment(scope.row.create_time).format("YYYY-MM-DD HH:mm:ss") }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sellPoint" label="商品卖点" show-overflow-tooltip> </el-table-column>
        <el-table-column label="商品描述" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ removeHTMLTag(scope.row.descs) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination :total="total" :pageSize="pageSize" @CurrentChange="CurrentChange"></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import moment from "moment";
import Pagination from "@/components/pagination/Pagination .vue";
import { removeHTMLTag } from "@/utils/common.js";
import { mapMutations } from "vuex";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      formInline: {
        user: "",
        date: "",
      },
      tableData: [],
      total: 1,
      pageSize: 1,
      current: 1,
      ids: [],
    };
  },
  created() {
    this.projectList();
  },
  methods: {
    moment,
    removeHTMLTag,
    ...mapMutations("product", ["changeRowData", "changeTitle"]),
    deleteAll() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let idStr = this.ids.join(",");
          this.$api.batchDelete({ ids: idStr }).then((res) => {
            console.log(res);
            if (res.data.status == 200) {
              let maxPage = Math.ceil(this.total / this.pageSize);
              let len = this.ids.length;
              if (maxPage == this.current) {
                let num =
                  this.total % this.pageSize == 0 ? this.pageSize : this.total % this.pageSize;
                if (num == len) {
                  this.current = this.current - 1 > 0 ? this.current - 1 : 1;
                }
              }
              this.projectList(1);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    selectHandle(selection) {
      let arr = [];
      selection.forEach((ele) => {
        arr.push(ele.id);
      });
      this.ids = arr;
    },
    toProductPage() {
      this.changeTitle("添加");
      this.$router.push("/product/add-product");
    },
    onSubmit() {
      this.search(this.formInline.user);
    },
    handleLook(index, row) {
      this.changeRowData(row);
      this.changeTitle("详情");
      this.$router.push("/product/add-product");
    },
    handleEdit(index, row) {
      this.changeRowData(row);
      this.changeTitle("编辑");
      this.$router.push("/product/add-product");
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteItemById(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    CurrentChange(val) {
      this.projectList(val);
      this.current = val;
    },
    blur() {
      if (!this.formInline.user) {
        this.projectList(1);
      }
    },
    async projectList(page) {
      let res = await this.$api.projectList({ page });
      this.tableData = res.data.data;
      this.total = res.data.total;
      this.pageSize = res.data.pageSize;
    },
    async search(search) {
      let res = await this.$api.search({ search });
      if (res.data.status == 200) {
        this.tableData = res.data.result;
        this.total = res.data.result.length;
        this.pageSize = res.data.result.length;
      } else {
        this.tableData = [];
        this.total = 1;
        this.pageSize = 1;
      }
    },
    async deleteItemById(id) {
      let res = await this.$api.deleteItemById({ id });
      console.log(res);
      if (res.data.status == 200) {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      }
      if (this.total % this.pageSize == 1) {
        this.current = this.current - 1 > 0 ? this.current - 1 : 1;
      }
      this.projectList(this.current);
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  background-color: #fff;
  margin-bottom: 20px;
  padding: 10px;

  .el-form-item {
    margin-bottom: 16px;
  }

  .group {
    border: 1px solid #eee;
    padding: 8px;
  }
}
.content {
  /deep/ .active-header {
    color: #333;
    text-align: center;
  }
  /deep/ .table-center {
    text-align: center;
  }

  .pagination {
    padding: 10px;
  }
}
</style>
